<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
    //获取设备分类
    public function getequipmenttype(){
        $list = db('equipmenttype')->where([])->order('createtime asc')->select();
        $this->success('ok',$list);
    }
    //获取分类下的设备
    public function getequipmentlist(){
        $equipmenttype_id = input('equipmenttype_id','');
        $where = [];
        if($equipmenttype_id){
            $where['equipmenttype_id'] = $equipmenttype_id;
        }
        $list = db('equipment')->field('id,name,image,introduce,labeljson')->where($where)->select();
        foreach($list as $k=>$v){
            $list[$k]['labeljson'] = json_decode($v['labeljson'],true);
        }
        $this->success('ok',$list);
    }
    //设备详情
    public function getequipmentinfo(){
        $equipment_id = input('equipment_id','');
        if(!$equipment_id){
            $this->error('缺失参数equipment_id');
        }
        $info = db('equipment')->where(['id'=>$equipment_id])->find();
        $info['labeljson'] = json_decode($info['labeljson'],true);
        $info['scenejson'] = json_decode($info['scenejson'],true);
        $data['info'] = $info;
        //设备参数
        $equipmentparam = db('equipmentparam')->where(['equipment_id'=>$equipment_id])->select();
        foreach($equipmentparam as $k=>$v){
            $equipmentparam[$k]['labeljson'] = json_decode($v['labeljson'],true);
        }
        $data['equipmentparam'] = $equipmentparam;
        $this->success('ok',$data);
    }
    //设备接口文档
    public function getequipmentapilist(){
        $equipment_id = input('equipment_id','');
        if(!$equipment_id){
            $this->error('缺失参数equipment_id');
        }
        $list = db('equipmentapi')->where(['equipment_id'=>$equipment_id])->order('weigh desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['headerjson'] = json_decode($v['headerjson'],true);
            $list[$k]['paramjson'] = json_decode($v['paramjson'],true);
            $list[$k]['responsejson'] = json_decode($v['responsejson'],true);
        }
        $this->success('ok',$list);
    }
}
